<%- # the license inside this block applies to this file
# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>

<%= lines(autogen_notice(:go, pwd)) -%>

<% resource_name = product_ns + object.name -%>

package google

import "fmt"

func Get<%= resource_name -%>IamPolicyCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
	return new<%= resource_name -%>IamAsset(d, config, expandIamPolicyBindings)
}

func Get<%= resource_name -%>IamBindingCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
	return new<%= resource_name -%>IamAsset(d, config, expandIamRoleBindings)
}

func Get<%= resource_name -%>IamMemberCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
	return new<%= resource_name -%>IamAsset(d, config, expandIamMemberBindings)
}

func Merge<%= resource_name -%>IamPolicy(existing, incoming Asset) Asset {
	existing.IAMPolicy = incoming.IAMPolicy
	return existing
}

func Merge<%= resource_name -%>IamBinding(existing, incoming Asset) Asset {
	return mergeIamAssets(existing, incoming, mergeAuthoritativeBindings)
}

func Merge<%= resource_name -%>IamBindingDelete(existing, incoming Asset) Asset {
	return mergeDeleteIamAssets(existing, incoming, mergeDeleteAuthoritativeBindings)
}

func Merge<%= resource_name -%>IamMember(existing, incoming Asset) Asset {
	return mergeIamAssets(existing, incoming, mergeAdditiveBindings)
}

func Merge<%= resource_name -%>IamMemberDelete(existing, incoming Asset) Asset {
	return mergeDeleteIamAssets(existing, incoming, mergeDeleteAdditiveBindings)
}

func new<%= resource_name -%>IamAsset(
	d TerraformResourceData,
	config *Config,
	expandBindings func(d TerraformResourceData) ([]IAMBinding, error),
) ([]Asset, error) {
	bindings, err := expandBindings(d)
	if err != nil {
		return []Asset{}, fmt.Errorf("expanding bindings: %v", err)
	}

	name, err := assetName(d, config, "//<%= product_ns.downcase -%>.googleapis.com/{{<%= object.name.downcase -%>}}")
	if err != nil {
		return []Asset{}, err
	}

	return []Asset{{
		Name: name,
		Type: "<%= product_ns.downcase -%>.googleapis.com/<%= object.name -%>",
		IAMPolicy: &IAMPolicy{
			Bindings: bindings,
		},
	}}, nil
}

func Fetch<%= resource_name -%>IamPolicy(d TerraformResourceData, config *Config) (Asset, error) {
	// Check if the identity field returns a value
	if _, ok := d.GetOk("{{<%= object.name.downcase -%>}}"); !ok {
		return Asset{}, ErrEmptyIdentityField
	}

	return fetchIamPolicy(
		<%= resource_name -%>IamUpdaterProducer,
		d,
		config,
		"//<%= product_ns.downcase  -%>.googleapis.com/{{<%= object.name.downcase -%>}}",
		"<%= product_ns.downcase  -%>.googleapis.com/<%= object.name -%>",
	)
}